package weaver.splittable.db.oracle;

import org.apache.commons.lang3.StringUtils;
import weaver.splittable.db.STCreateIndexUtil;
import weaver.splittable.db.STDBConstants;

import java.util.List;

/**
 * 建索引工具的Oracle实现
 * author: YUEYANG 2023-01-12
 */
public class STOracleCreateIndexUtil implements STCreateIndexUtil {

    private static final String CREATE_CHANGE_INDEX_ID = "create index idx_dci_id on "+ STDBConstants.DATA_CHANGE_TABLE_NAME +"(id) online";
    private static final String CREATE_CHANGE_INDEX_DATAID = "create index idx_dci_dataid on "+ STDBConstants.DATA_CHANGE_TABLE_NAME +"(data_id) online";
    private static final String CREATE_CHANGE_INDEX_SYNCED = "create index idx_dci_synced on "+ STDBConstants.DATA_CHANGE_TABLE_NAME +"(synced) online";
    private static final String CREATE_CHANGE_INDEX_SYNCED_TABLENAME_EVENTTYPE = "create index idx_dci_stnet on "+ STDBConstants.DATA_CHANGE_TABLE_NAME +"(synced, table_name, event_type) online";
    private static final String CREATE_CHANGE_INDEX_SYNCED_SYNCTIME = "create index idx_dci_ss on "+ STDBConstants.DATA_CHANGE_TABLE_NAME +"(synced, sync_time) online";

    private static final String CREATE_CLEAR_INDEX_ID = "create index idx_jcdi_synced on "+ STDBConstants.DATA_CLEAR_TABLE_NAME +"(id) online";
    private static final String CREATE_CLEAR_INDEX_DATAID = "create index idx_jcdi_dataid on "+ STDBConstants.DATA_CLEAR_TABLE_NAME +"(data_id) online";
    private static final String CREATE_CLEAR_INDEX_SYNCED = "create index idx_jcdi_synced on "+ STDBConstants.DATA_CLEAR_TABLE_NAME +"(synced) online";
    private static final String CREATE_CLEAR_INDEX_SYNCED_TABLENAME_DATAID = "create index idx_jcdi_synced_tn_di on "+ STDBConstants.DATA_CLEAR_TABLE_NAME +"(synced, table_name, data_id) online";
    private static final String CREATE_CLEAR_INDEX_SYNCED_SYNCTIME = "create index idx_jcdi_ss on "+ STDBConstants.DATA_CLEAR_TABLE_NAME +"(synced, sync_time) online";

    private static final String CREATE_INDEX_SQL = "create index %s on %s (%s) online";

    @Override
    public String createChangeIndex_Id() {
        return CREATE_CHANGE_INDEX_ID;
    }

    @Override
    public String createChangeIndex_DataId() {
        return CREATE_CHANGE_INDEX_DATAID;
    }

    @Override
    public String createChangeIndex_Synced() {
        return CREATE_CHANGE_INDEX_SYNCED;
    }

    @Override
    public String createChangeIndex_SyncedTableNameEventType() {
        return CREATE_CHANGE_INDEX_SYNCED_TABLENAME_EVENTTYPE;
    }

    @Override
    public String createChangeIndex_SyncedSyncTime() {
        return CREATE_CHANGE_INDEX_SYNCED_SYNCTIME;
    }

    @Override
    public String createClearIndex_Id() {
        return CREATE_CLEAR_INDEX_ID;
    }

    @Override
    public String createClearIndex_DataId() {
        return CREATE_CLEAR_INDEX_DATAID;
    }

    @Override
    public String createClearIndex_Synced() {
        return CREATE_CLEAR_INDEX_SYNCED;
    }

    @Override
    public String createClearIndex_SyncedTableNameDataId() {
        return CREATE_CLEAR_INDEX_SYNCED_TABLENAME_DATAID;
    }

    @Override
    public String createClearIndex_SyncedSyncTime() {
        return CREATE_CLEAR_INDEX_SYNCED_SYNCTIME;
    }

    @Override
    public String createIndex(String indexName, String tableName, List<String> columnNames) {
        String columns = StringUtils.join(columnNames, ",");
        return String.format(CREATE_INDEX_SQL, indexName, tableName, columns);
    }

}
